package com.lam.common.components.logger;

import org.slf4j.MDC;

import com.lam.common.context.ContextHolder;
import com.lam.common.context.SecurityUtils;
import com.lam.common.utils.StringUtils;

import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;

public class RequestTrace extends ClassicConverter {

    @Override
    public String convert(ILoggingEvent event) {
        
        String username = SecurityUtils.getLoginUsername();
        if(StringUtils.isBlank(username)) {
        	username = "anonymous";
        }
        String requestTrace = String.format("reqid:%s oper:%s", ContextHolder.getTraceId(), username);
        MDC.put("requestTrace", requestTrace);
        return requestTrace;
    }
    
}

